package com.jmxcfc.blfsc.ssq.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jmxcfc.blfsc.common.common.request.BaseRequest;
import com.jmxcfc.blfsc.common.common.response.BaseResponse;
import com.jmxcfc.blfsc.common.sign.SignRequest;
import com.jmxcfc.blfsc.common.sign.extend.SignExtend;
import com.jmxcfc.blfsc.ssq.entity.*;
import com.jmxcfc.blfsc.ssq.entity.vo.UploadEntSealImgExtend;
import com.jmxcfc.blfsc.ssq.service.contract.IDownloadContractRequestService;
import com.jmxcfc.blfsc.ssq.service.contract.ILockContractRequestService;
import com.jmxcfc.blfsc.ssq.service.contract.ISignContractRequestService;
import com.jmxcfc.blfsc.ssq.service.contract.IUploadContractRequestService;
import com.jmxcfc.blfsc.ssq.service.reguser.IQueryUserApplyCertRequestService;
import com.jmxcfc.blfsc.ssq.service.reguser.IRegUserRequestService;
import com.jmxcfc.blfsc.ssq.service.sealimg.IUploadSealImgRequestService;
import com.jmxcfc.blfsc.ssq.service.usersignature.ICreateUserSignatureRequestService;
import com.jmxcfc.fs.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * cfca请求控制器
 *
 * 1、需要企业章信息的接口:
 * (1)上传企业章信息接口:uploadSealImg 方法,需要企业account账号、企业章的图片路径
 * (2)签署合同接口:signContract 方法,需要企业account账号、签章位置等相关信息
 */
@RestController
@RequestMapping("/ssq")
@Slf4j
public class SsqRequestController {

    private IRegUserRequestService regUserRequestService;
    private IQueryUserApplyCertRequestService queryUserApplyCertRequestService;
    private ICreateUserSignatureRequestService signatureRequestService;
    private IUploadSealImgRequestService uploadSealImgRequestService;
    private IUploadContractRequestService uploadContractRequestService;
    private ISignContractRequestService signContractRequestService;
    private ILockContractRequestService lockContractRequestService;
    private IDownloadContractRequestService downloadContractRequestService;



    @Autowired
    public void setRegUserRequestService(IRegUserRequestService regUserRequestService) {
        this.regUserRequestService = regUserRequestService;
    }


    @Autowired
    public void setQueryUserApplyCertRequestService(IQueryUserApplyCertRequestService queryUserApplyCertRequestService) {
        this.queryUserApplyCertRequestService = queryUserApplyCertRequestService;
    }


    @Autowired
    public void setSignatureRequestService(ICreateUserSignatureRequestService signatureRequestService) {
        this.signatureRequestService = signatureRequestService;
    }


    @Autowired
    public void setUploadSealImgRequestService(IUploadSealImgRequestService uploadSealImgRequestService) {
        this.uploadSealImgRequestService = uploadSealImgRequestService;
    }

    @Autowired
    public void setUploadContractRequestService(IUploadContractRequestService uploadContractRequestService) {
        this.uploadContractRequestService = uploadContractRequestService;
    }

    @Autowired
    public void setSignContractRequestService(ISignContractRequestService signContractRequestService) {
        this.signContractRequestService = signContractRequestService;
    }


    @Autowired
    public void setLockContractRequestService(ILockContractRequestService lockContractRequestService) {
        this.lockContractRequestService = lockContractRequestService;
    }

    @Autowired
    public void setDownloadContractRequestService(IDownloadContractRequestService downloadContractRequestService) {
        this.downloadContractRequestService = downloadContractRequestService;
    }
    /**
     * 步骤1:注册个人用户并申请证书
     * @param request
     * @return
     */
    @PostMapping("/sign/regUser")
    public Result<BaseResponse<RegUserPersonalSub>> regUserRequest(@RequestBody BaseRequest<SignRequest<JSONObject>> request) {
        log.info("注册个人用户并申请证书请求,请求参数:{}", JSON.toJSONString(request));
        BaseResponse<RegUserPersonalSub> response = regUserRequestService.sendRequest(request);
        log.info("注册个人用户并申请证书请求,响应结果:{}", JSON.toJSONString(response));
        return Result.success(response);
    }


    /**
     * 步骤2:查询用户证书申请状态请求
     * @param request
     * @return
     */
    @PostMapping("/sign/queryRegUser")
    public Result<BaseResponse<QueryUserApplyCertSub>> queryUserApplyCert(@RequestBody BaseRequest<SignRequest<JSONObject>> request) {
        log.info("查询用户证书申请状态请求,请求参数:{}", JSON.toJSONString(request));
        BaseResponse<QueryUserApplyCertSub> response = queryUserApplyCertRequestService.sendRequest(request);
        log.info("查询用户证书申请状态请求,响应结果:{}", JSON.toJSONString(response));
        return Result.success(response);
    }


    /**
     * 步骤3(个人章):上上签创建用户签名
     * @param request
     * @return
     */
    @PostMapping("/sign/createUser")
    public Result<BaseResponse<CreateUserSignatureSub>> createUser(@RequestBody BaseRequest<SignRequest<JSONObject>> request) {
        log.info("创建用户签名请求,请求参数:{}", JSON.toJSONString(request));
        BaseResponse<CreateUserSignatureSub> response = signatureRequestService.sendRequest(request);
        log.info("创建用户签名请求,响应结果:{}", JSON.toJSONString(response));
        return Result.success(response);
    }


    /**
     * 步骤3(企业章):上上签上传企业印章图片非每次调用,一个企业印章调用一次即可
     * @param request
     * @return
     */
    @PostMapping("/sign/uploadSealImg")
    public Result<BaseResponse<UploadSealImgSub>> uploadSealImg(@RequestBody BaseRequest<SignRequest<UploadEntSealImgExtend>> request) {
        log.info("上传企业印章图片请求,请求参数:{}", JSON.toJSONString(request));
        BaseResponse<UploadSealImgSub> response = uploadSealImgRequestService.sendRequest(request);
        log.info("上传企业印章图片请求,响应结果:{}", JSON.toJSONString(response));
        return Result.success(response);
    }

    /**
     *步骤4:上传并创建合同请求
     * @param request
     * @return
     */
    @PostMapping("/sign/uploadContract")
    public Result<BaseResponse<UploadContractSub>> uploadContract(@RequestBody BaseRequest<SignRequest<SignExtend>> request) {
        log.info("上传并创建合同请求,请求参数:{}", JSON.toJSONString(request));
        BaseResponse<UploadContractSub> response = uploadContractRequestService.sendRequest(request);
        log.info("传并创建合同请求,响应结果:{}", JSON.toJSONString(response));
        return Result.success(response);
    }


    /**
     * 步骤5签署合同请求
     * 1、个人签章:账号字段取值 idCode
     *2、 扩展字段必传:
      签署类型signType
     个人签章:signX、signY、signPage、
     企业签章:signXEnt、signYEnt、signPageEnt、sealAccount(企业章账号)
     *3、SignType.TYPE_1:签署个人签名企业签名 需要调用两次接口
     * @param request
     * @return
     */
    @PostMapping("/sign/signContract")
    public Result<BaseResponse<SignContractSub>> signContract(@RequestBody BaseRequest<SignRequest<SignExtend>> request) {
        log.info("签署合同请求,请求参数:{}", JSON.toJSONString(request));
        BaseResponse<SignContractSub> response = signContractRequestService.sendRequest(request);
        log.info("签署合同请求,响应结果:{}", JSON.toJSONString(response));
        return Result.success(response);
    }


    /**
     * 步骤6 锁定合同
     * @param request
     * @return
     */
    @PostMapping("/sign/lockedContract")
    public Result<BaseResponse<LockContractSub>> lockedContract(@RequestBody BaseRequest<SignRequest<String>> request) {
        log.info("锁定并结束合同请求,请求参数:{}", JSON.toJSONString(request));
        BaseResponse<LockContractSub> response = lockContractRequestService.sendRequest(request);
        log.info("锁定并结束合同请求,响应结果:{}", JSON.toJSONString(response));
        return Result.success(response);
    }

    /**
     * 下载合同
     * @param request
     * @return
     */
    @PostMapping("/sign/downloadContract")
    public Result<BaseResponse<JSONObject>> downloadContract(@RequestBody BaseRequest<SignRequest<String>> request) {
        log.info("下载合同文件请求,请求参数:{}", JSON.toJSONString(request));
        BaseResponse<JSONObject> response = downloadContractRequestService.sendRequest(request);
        log.info("下载合同文件请求,响应结果:{}", JSON.toJSONString(response));
        return Result.success(response);
    }

}
